﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2017/2/4
 * 时间: 21:07
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.IO;

namespace MiniHttpServer
{
	/// <summary>
	/// Description of HttpFile.
	/// </summary>
	public class HttpFile
	{
		public HttpFile()
		{
		}
		
		public static string GetFilePath(string wwwRootPath, string fileUrl)
		{
			string filePath = wwwRootPath;
			filePath += fileUrl.Replace('/', Path.DirectorySeparatorChar);
			return filePath;
		}
		
		public static byte[] GetFileContent(string filePath)
		{
			if (!File.Exists(filePath)) {
				return null;
			}
			var br = new BinaryReader(new FileStream(filePath, FileMode.Open));
			var fileContent = new byte[br.BaseStream.Length];
			br.Read(fileContent, 0, (int)br.BaseStream.Length);
			br.Close();
			return fileContent;
		}
		
		public static string GetUrlFile(string rawUrl)
		{
			string urlFile = rawUrl;
			int pos = rawUrl.IndexOf("?", StringComparison.Ordinal);
			if (pos >= 0)
			{
				urlFile = rawUrl.Substring(0, pos);
			}
			return urlFile;
		}
		
		public static string GetUrlFileExtName(string urlFile)
		{
			int pos = urlFile.LastIndexOf('.');
			string extFname = "";
			if (pos >= 0)
			{
				extFname = urlFile.Substring(pos).ToLower();
			}
			return extFname;
		}
	}
}
